package com.example.chatapp11

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.chatapp11.databinding.ActivityConnectBinding

class ConnectActivity : AppCompatActivity() {
    private lateinit var binding: ActivityConnectBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityConnectBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 设置默认值
        binding.etIpAddress.setText("10.120.16.167") // 保留原来的默认IP
        binding.etPort.setText("20000") // 保留原来的默认端口

        // 连接按钮点击事件
        binding.btnConnect.setOnClickListener {
            val ipAddress = binding.etIpAddress.text.toString().trim()
            val port = binding.etPort.text.toString().trim()
            val nickname = binding.etNickname.text.toString().trim()

            if (ipAddress.isNotEmpty() && port.isNotEmpty()) {
                // 跳转到主聊天界面，并传递IP、端口和昵称
                val intent = Intent(this, MainActivity::class.java)
                intent.putExtra("SERVER_IP", ipAddress)
                intent.putExtra("SERVER_PORT", port.toInt())
                intent.putExtra("USER_NICKNAME", nickname.ifEmpty { "匿名用户" })
                startActivity(intent)
            }
        }
    }
}